﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using AuthenticationService.Client;
using System.Security.Principal;
using System.Windows.Ria.ApplicationServices;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Infrastructure;
using UserManager.Module.Views;

namespace UserManager.Module.ViewModels
{
    public class LoginViewModel : INotifyPropertyChanged
    {
        WebContext context;
        private readonly IEventAggregator _eventAggregator;
        private SubscriptionToken _subscriptionToken;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;

        public LoginViewModel(IUnityContainer container, IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            _container = container;
            _eventAggregator = eventAggregator;
            _regionManager = regionManager;
            LogonCommand = new DelegateCommand<object>(Logon, CanLogon);
            CancelCommand = new DelegateCommand<object>(Cancel, CanCancel);
            context = WebContext.Current;
            context.Authentication.LoggedIn += new EventHandler<System.Windows.Ria.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedIn);
            context.Authentication.LoggedOut += new EventHandler<System.Windows.Ria.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        void Authentication_LoggedOut(object sender, System.Windows.Ria.ApplicationServices.AuthenticationEventArgs e)
        {
        }

        void Authentication_LoggedIn(object sender, System.Windows.Ria.ApplicationServices.AuthenticationEventArgs e)
        {
        }

        private string username = null;
        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
                NotifyPropertyChanged("Username");
            }
        }

        private string password = null;
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                NotifyPropertyChanged("Password");
            }
        }

        private void CloseLoginView()
        {
            LoginView loginView = _container.Resolve<LoginView>("LoginWindow");
            loginView.Close();
        }

        #region Commands
        public DelegateCommand<object> LogonCommand { get; private set; }

        public void Logon(object commandArg)
        {
            LoginOperation loginResult = context.Authentication.Login(Username, Password);
            loginResult.Completed += new EventHandler(loginResult_Completed);
        }

        void loginResult_Completed(object sender, EventArgs e)
        {
            LoginOperation loginResult = sender as LoginOperation;

            CloseLoginView();
        }

        public bool CanLogon(object commandArg)
        {
            return true;
        }

        public DelegateCommand<object> CancelCommand { get; private set; }

        public void Cancel(object commandArg)
        {
            CloseLoginView();
        }

        public bool CanCancel(object commandArg)
        {
            return true;
        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
